package com.dkm.ec.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.gl.persistence.dao.GlCateQueryDao;
import com.dkm.biz.gl.persistence.entity.GlCatePo;
import com.dkm.ec.product.persistence.entity.ProductMediaPo;
import com.dkm.ec.product.service.ProductMediaService;

@Service
public class ProductMediaServiceImpl implements ProductMediaService{
	@Resource
	private GlCateQueryDao glCateQueryDao;
	
	public void setCateKeys(List<ProductMediaPo> productMediaPos){
		List<String> cateIds = getCateIds(productMediaPos);
		Map<String,String> cateKeyMap = findCateKeyMap(cateIds);
		for(ProductMediaPo productMediaPo:productMediaPos){
			if(StringUtils.isNotEmpty(productMediaPo.getCateId())){
				productMediaPo.setCateKey(cateKeyMap.get(productMediaPo.getCateId()));
			}
		}		
	}
	
	private List<String> getCateIds(List<ProductMediaPo> productMediaPos) {
		List<String> cateIds = new ArrayList<String>();
		for(ProductMediaPo productMediaPo:productMediaPos){
			if(StringUtils.isNotEmpty(productMediaPo.getCateId()))
				cateIds.add(productMediaPo.getCateId());
		}
		return cateIds;
	}
	
	private Map<String,String> findCateKeyMap(List<String> cateIds){
		List<GlCatePo> glCatePos =  glCateQueryDao.findByIds(cateIds);
		Map<String, String> cateKeys = new HashMap<String, String>();
		for(GlCatePo glCatePo:glCatePos){
			cateKeys.put(glCatePo.getId(), glCatePo.getKey());
		}
		return cateKeys;
	}
}
